#include "keybutton.h"

#include <QApplication>
#include <QMouseEvent>

Keybutton::Keybutton(QWidget *parent):QPushButton(parent)
{
    connect(this,&QPushButton::clicked,[&](){
        QString str=this->text();
        char c=str[0].toLatin1();
        //连接事件
        QKeyEvent*ke=new QKeyEvent(QKeyEvent::KeyPress,c,Qt::NoModifier,str);
        //发送事件
        QApplication::postEvent(QApplication::focusWidget(),ke);
    });
}

Keybutton::Keybutton(const QString &text, QWidget *parent):QPushButton(text,parent)
{
    connect(this,&QPushButton::clicked,[&](){//在构造函数中，连接按键事件为发送事件
        QString str=this->text();
        char c=str[0].toLatin1();
        //连接事件
        QKeyEvent*ke=new QKeyEvent(QKeyEvent::KeyPress,c,Qt::NoModifier,str);
        //发送事件
        QApplication::postEvent(QApplication::focusWidget(),ke);
    });
}
